package love.yuanmeng.guetcoalsystem.cotroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.sun.org.glassfish.gmbal.ParameterNames;
import love.yuanmeng.guetcoalsystem.pojo.EnterpriseInfo;
import love.yuanmeng.guetcoalsystem.pojo.User;
import love.yuanmeng.guetcoalsystem.service.EnterpriseInfoService;
import love.yuanmeng.guetcoalsystem.service.UserService;
import love.yuanmeng.guetcoalsystem.util.DataGridView;
import love.yuanmeng.guetcoalsystem.util.ResultObj;
import love.yuanmeng.guetcoalsystem.vo.UserAndMessage;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/9/26 12:21
 */
@CrossOrigin
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    EnterpriseInfoService enterpriseInfoService;

    /**
     *
     */
    @GetMapping("/getUser")
    public ModelAndView getHealthPreservation(Integer current, Integer limit) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("myinfo/myinfo");
        modelAndView.addObject("user", "1123");
        return modelAndView;
    }

    /**
     * 获取交易用户列表
     *
     * @param current
     * @param size
     * @param user
     * @return
     */
    @RequestMapping("/getTradeUser")
    public IPage getTradeUser(@RequestParam int current, @RequestParam int size, @RequestBody User user) {
        Page<User> page = new Page<User>();
        page.setCurrent(current);
        page.setSize(size);
        return userService.getTradeUser(page, user);
    }

    @RequestMapping("/getEnterpriseInfoById")
    public EnterpriseInfo getEnterpriseInfoById(long enterpriseId) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoService.getEnterpriseInfoById(enterpriseId);
        return enterpriseInfo;
    }

    /**
     * 企业资质审核
     * @param userAndMessage
     * @return
     */
    @RequestMapping("/approvedUser")
    public ResultObj approvedUser(@RequestBody UserAndMessage userAndMessage) {
        boolean b = userService.approvedUser(userAndMessage.getUser(), userAndMessage.getMessage());
        return b ? ResultObj.UPDATE_SUCCESS : ResultObj.UPDATE_ERROR;
    }


//    /**
//     * 文件下载
//     * @param fileName
//     * @param res
//     */
//    @RequestMapping("/download")
//    public void downloadFile(@RequestParam String fileName, HttpServletResponse res) {
//        fileName = "C:\\Users\\MookJT\\Desktop\\1632383334131440.png";
//        res.reset();
//        res.setHeader("content-type", "application/octet-stream");
//        res.setContentType("application/octet-stream");
//        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//        byte[] buff = new byte[1024];
//        BufferedInputStream bis = null;
//        OutputStream os = null;
//        try {
//            os = res.getOutputStream();
//            bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
//            int i = bis.read(buff);
//            while (i != -1) {
//                os.write(buff, 0, buff.length);
//                os.flush();
//                i = bis.read(buff);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (bis != null) {
//                try {
//                    bis.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        System.out.println("success");
//    }




    /**
     * 文件下载
     * @param fileName
     * @param res
     */
    @RequestMapping("/download")
    public void downloadFile(@RequestParam String fileName, HttpServletResponse res,HttpServletRequest request) throws IOException {
        fileName = "C:\\Users\\MookJT\\Desktop\\1632383334131440.png";
        System.out.println(fileName);
        //获取输入流对象（用于读文件）
        File file = new File(fileName);
        if(!file.exists()){
            return;
        }
        FileInputStream fis = new FileInputStream(file);
        //获取文件后缀
        String extendFileName = fileName.substring(fileName.lastIndexOf('.'));
        //动态设置响应类型，根据前台传递文件类型设置响应类型
        res.reset();
        res.setContentType(request.getSession().getServletContext().getMimeType(extendFileName));
        res.setHeader("content-disposition", "attachment;filename=" + fileName);
        ServletOutputStream os = res.getOutputStream();
        FileCopyUtils.copy(fis,os);
    }


}
